<?php
/**
 *
 * @package    opensocial
 * @subpackage datarequest
 * @author     Daniel Botelho <botelho.daniel@gmail.com>
 */
class OSFetchPersonAppDataRequest implements OSDataRequestable
{ 

  const NEWFETCH_PERSONAPPDATA_REQUEST_PATTERN = "newFetchPersonAppDataRequest(%s,%s)";

  private $appDataListVarName;
  private $appDataList = array();
  private $person;

  function __construct(OSViewer $person)
  {
    $this->person = $person;
    $this->appDataListVarName = $this->getOptionalKey()."_fields";
  }
  
  public function getAppDataListVarName(){
  	return $this->appDataListVarName;
  }
  
  public function generateDataRequest(OSDataRequest $dataRequest)
  {
  	//vai buscar o nome da variável que é usada para o dataRequest
    $request_var = $dataRequest->getDataRequestVarName();

    $html = 'var '.$this->getAppDataListVarName()." = new Array();\n";

    foreach($this->appDataList as $appData) {
      $html .= $this->getAppDataListVarName().".push(\"". $this->getKey() ."\");\n";
    }

    $html .= $request_var.".add(".$request_var.".".sprintf(OSFetchPersonAppDataRequest::NEWFETCH_PERSONAPPDATA_REQUEST_PATTERN,$this->getPerson()->getType(),$this->getAppDataListVarName()).",\"".$this->getOptionalKey()."\");\n";
  	return $html;
  }
  
  public function getOptionalKey()
  {
  	return $this->getPerson()->getVarName()."_data";
  }
  
  //FIXME: isto ainda nao esta implementado
  function fetchPersonAppData(OSPersonAppData $appData)
  {
  	$appDataList[] = $appData;
  }
  
  public function getPerson()
  {
    return $this->person;
  }

  
}
